package com.lineage.server.model;

import com.lineage.server.model.Instance.L1MonsterInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_DoActionGFX;
import com.lineage.server.serverpackets.S_Paralysis;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.thread.GeneralThreadPool;
import java.util.TimerTask;
import java.util.concurrent.ScheduledFuture;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class L1Cube extends TimerTask {
  private static final Log _log = LogFactory.getLog(L1Cube.class);
  
  private ScheduledFuture<?> _future;
  
  private int _timeCounter;
  
  private final L1Character _effect;
  
  private final L1Character _cha;
  
  private final int _skillId;
  
  public L1Cube(L1Character effect, L1Character cha, int skillId) {
    this._future = null;
    this._timeCounter = 0;
    this._effect = effect;
    this._cha = cha;
    this._skillId = skillId;
  }
  
  public void run() {
    try {
      if (this._cha.isDead()) {
        stop();
        return;
      } 
      if (!this._cha.hasSkillEffect(this._skillId)) {
        stop();
        return;
      } 
      this._timeCounter++;
      giveEffect();
    } catch (Throwable e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public void begin() {
    this._future = GeneralThreadPool.get().scheduleAtFixedRate(this, 900L, 1000L);
  }
  
  public void stop() {
    if (this._future != null)
      this._future.cancel(false); 
  }
  
  public void giveEffect() {
    switch (this._skillId) {
      case 1019:
        if (this._timeCounter % 4 != 0)
          return; 
        if (this._cha.hasSkillEffect(4000))
          return; 
        if (this._cha.hasSkillEffect(78))
          return; 
        if (this._cha.hasSkillEffect(50))
          return; 
        if (this._cha.hasSkillEffect(157))
          return; 
        if (this._cha instanceof L1PcInstance) {
          L1PcInstance pc = (L1PcInstance)this._cha;
          pc.sendPacketsAll((ServerBasePacket)new S_DoActionGFX(pc.getId(), 2));
          pc.receiveDamage(this._effect, 10.0D, false, true);
          break;
        } 
        if (this._cha instanceof L1MonsterInstance) {
          L1MonsterInstance mob = (L1MonsterInstance)this._cha;
          mob.broadcastPacketX10((ServerBasePacket)new S_DoActionGFX(mob.getId(), 2));
          mob.receiveDamage(this._effect, 10);
        } 
        break;
      case 1021:
        if (this._timeCounter % 4 != 0)
          return; 
        if (this._cha.hasSkillEffect(4000))
          return; 
        if (this._cha.hasSkillEffect(78))
          return; 
        if (this._cha.hasSkillEffect(50))
          return; 
        if (this._cha.hasSkillEffect(157))
          return; 
        if (this._cha instanceof L1PcInstance) {
          L1PcInstance pc = (L1PcInstance)this._cha;
          pc.setSkillEffect(4000, 1000);
          pc.sendPackets((ServerBasePacket)new S_Paralysis(6, true));
          break;
        } 
        if (this._cha instanceof L1MonsterInstance) {
          L1MonsterInstance mob = (L1MonsterInstance)this._cha;
          mob.setSkillEffect(4000, 1000);
          mob.setPassispeed(0);
        } 
        break;
      case 1023:
        this._cha.setSkillEffect(1024, 4000);
        break;
      case 1025:
        if (this._timeCounter % 4 == 0) {
          int newMp = this._cha.getCurrentMp() + 5;
          if (newMp < 0)
            newMp = 0; 
          this._cha.setCurrentMp(newMp);
        } 
        if (this._timeCounter % 5 != 0)
          break; 
        if (this._cha instanceof L1PcInstance) {
          L1PcInstance pc = (L1PcInstance)this._cha;
          pc.receiveDamage(this._effect, 25.0D, false, true);
          break;
        } 
        if (this._cha instanceof L1MonsterInstance) {
          L1MonsterInstance mob = (L1MonsterInstance)this._cha;
          mob.receiveDamage(this._effect, 25);
        } 
        break;
    } 
  }
}
